package com.asm.service.impl;

import com.asm.mapper.SNV2TfCHIPseqMapper;
import com.asm.pojo.SNV2TfCHIPseq;
import com.asm.service.SNV2TfCHIPseqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SNV2TfCHIPseqServiceImpl implements SNV2TfCHIPseqService {
    @Autowired
    private SNV2TfCHIPseqMapper snv2TfCHIPseqMapper;

    @Override
    public List<SNV2TfCHIPseq> getDetailTFbindingBySnv(String snv) {
        ArrayList<SNV2TfCHIPseq> result = new ArrayList<>();
        List<String> tableList = getindexBysnv(snv);
        System.out.println(tableList.toString());
        for (String table: tableList) {
            List<String> samples = snv2TfCHIPseqMapper.getSamples(snv, table);
            System.out.println(samples);
            if (samples.isEmpty()){
                result.addAll(new ArrayList<>());
            }else {
                result.addAll(snv2TfCHIPseqMapper.getDetailTFbindingBySnv(snv, table, samples));
            }
        }
        return result;
    }

    @Override
    public List<SNV2TfCHIPseq> getChIPseqAnalysis1(String tf,String snv,String[] health_status,String tissue) {
//        List<String> samples = snv2TfCHIPseqMapper.getSamplesBytf(tf);
//        ArrayList<SNV2TfCHIPseq> result = new ArrayList<>();
        List<String> indexList = null;
        if (tf!=null){
            indexList = getindexByTf(tf);
        }
        if (snv!=null){
            indexList = getindexBysnv(snv);
        }

        List<SNV2TfCHIPseq> result = indexList.stream()
                .map(index -> snv2TfCHIPseqMapper.getChIPseqAnalysis1(tf,index,snv,health_status,tissue))
                .flatMap(Collection::stream)
                .collect(Collectors.toList());
        return result;
    }

    @Override
    public List<SNV2TfCHIPseq> getChIPseqAnalysis1BySearch(String tf,String snv,String[] health_status,String tissue, String search) {
        List<String> indexList = null;
        if (tf!=null){
            indexList = getindexByTf(tf);
        }
        if (snv!=null){
            indexList = getindexBysnv(snv);
        }

        List<SNV2TfCHIPseq> result = indexList.stream()
                .map(index -> snv2TfCHIPseqMapper.getChIPseqAnalysis1BySearch(tf,index,snv,health_status,tissue,search))
                .flatMap(Collection::stream)
                .collect(Collectors.toList());

        return result;
    }

    public List<String> getindexBysnv(String snv) {
        List<String> index = snv2TfCHIPseqMapper.getindexBysnv(snv);
        List<String> indexArray = new ArrayList<>();
        for (String s : index) {
            indexArray.add("detail_tf_"+s);
        }
        return indexArray;
    }

    public List<String> getindexByTf(String tf){
        List<String> list = snv2TfCHIPseqMapper.getindexBysample(tf);
//        索引表
        List<String> arrayList = list.stream()
                .map(s -> "analysis1_tf_"+s)
                .collect(Collectors.toList());
        return arrayList;
    }


}
